package com.zhongyewx.teachercert.ui;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.zhongyewx.teachercert.R;
import com.zhongyewx.teachercert.api.ZYARouterApi;
import com.zhongyewx.teachercert.base.ZYBaseActivity;
import com.zhongyewx.teachercert.ui.fragment.ZYHomeFragment;
import com.zhongyewx.teachercert.ui.fragment.ZYTwoFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ZYARouterApi.AROUTER_MAIN_ACTY, extras = ZYARouterApi.AROUTER_NEED_LOGIN)
public class MainActivity extends ZYBaseActivity{

    @BindView(R.id.view_home)
    View viewHome;
    @BindView(R.id.view_two)
    View viewTwo;
    /**
     * 用于对Fragment进行管理
     */
    private FragmentManager fragmentManager;
    private ZYHomeFragment homeFragment;
    private static final String TAG_home = "home";
    private static final String TAG_two = "two";
    private String currentFragTag = TAG_home;// 当前fragment的tag

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);

        homeFragment = new ZYHomeFragment();
        fragmentManager = getSupportFragmentManager();
        // 第一次启动时选中TAG_home

        fragmentManager.beginTransaction()
                .replace(R.id.home_frame, homeFragment, TAG_home).commitAllowingStateLoss();
    }

    @Override
    public void initData() {
//        ptrFrameLayout.autoRefresh(true, 400);

    }

    @Override
    public void request() {
    }


    @OnClick({R.id.view_home, R.id.view_two})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.view_home:
                switchFrag(currentFragTag, TAG_home);
                break;
            case R.id.view_two:
                switchFrag(currentFragTag, TAG_two);
                break;
        }
    }


    private void switchFrag(String preFrag, String fragTag) {
        if (!preFrag.equals(fragTag)) {
            // 查找fragment栈，看是否该fragment已存在，存在show,不存在则新建
            Fragment tempFragment = fragmentManager.findFragmentByTag(fragTag);
            Fragment preFragment = fragmentManager.findFragmentByTag(preFrag);
            if (tempFragment == null) {
                String tagStr = "";
                Fragment storeFrag = null;
                switch (fragTag) {
                    case TAG_home:
                        tagStr = TAG_home;
                        storeFrag = new ZYHomeFragment();
                        break;
                    case TAG_two:
                        tagStr = TAG_two;
                        storeFrag = new ZYTwoFragment();
                        break;
                    default:
                        break;
                }
                fragmentManager.beginTransaction().hide(preFragment)
                        .add(R.id.home_frame, storeFrag, tagStr).commitAllowingStateLoss();

            } else {
                fragmentManager.beginTransaction().hide(preFragment)
                        .show(tempFragment).commitAllowingStateLoss();
            }
            switchImg(fragTag);// 底部导航图片高亮显示
        }
        currentFragTag = fragTag;
    }

    // 底部TextView切换高亮
    private void switchImg(String fargTag) {
        switch (currentFragTag) {
            case TAG_home:
                viewHome.setBackgroundColor(getResources().getColor(R.color.color_131e32));
                break;
            case TAG_two:
                viewTwo.setBackgroundColor(getResources().getColor(R.color.color_131e32));
                break;

            default:
                break;
        }

        switch (fargTag) {
            case TAG_home:
                viewHome.setBackgroundColor(getResources().getColor(R.color.color_ff9464));
                break;
            case TAG_two:
                viewTwo.setBackgroundColor(getResources().getColor(R.color.color_ff9464));
                break;
            default:
                break;
        }
    }

}
